<?php

//折翼天使资源社区 提供 www.zheyitianshi.com
if (!defined('IN_IA')) {
	die('Access Denied');
}
global $_W, $_GPC;
$operation = !empty($_GPC['op']) ? $_GPC['op'] : 'display';
$openid = m('user')->getOpenid();
$mid = m('member')->getMid();
$uniacid = $_W['uniacid'];
$agentLevel = $this->model->getLevel($openid);
$level = intval($this->set['level']);
if ($_W['isajax']) {
	if ($operation == 'display') {
		$pindex = max(1, intval($_GPC['page']));
		$psize = 20;
		$condition = " and `mid`=:mid and uniacid=:uniacid";
		$params = array(':mid' => $mid, ':uniacid' => $uniacid);
		$status = trim($_GPC['status']);
		if ($status != '') {
			$condition .= ' and status=' . intval($status);
		}
		$commissioncount = 0;
		$list = pdo_fetchall("select * from " . tablename('ewei_shop_commission_apply') . " where 1 {$condition} order by id desc LIMIT " . ($pindex - 1) * $psize . ',' . $psize, $params);
		$total = pdo_fetchcolumn('select count(*) from ' . tablename('ewei_shop_commission_apply') . " where 1 {$condition}", $params);
		foreach ($list as &$row) {
			$commissioncount += $row['commission'];
			if ($row['status'] == 1) {
				$row['statusstr'] = '待审核';
				$row['dealtime'] = date('Y-m-d H:i', $row['applytime']);
			} else {
				if ($row['status'] == 2) {
					$row['statusstr'] = '待打款';
					$row['dealtime'] = date('Y-m-d H:i', $row['checktime']);
				} else {
					if ($row['status'] == 3) {
						$row['statusstr'] = '已打款';
						$row['dealtime'] = date('Y-m-d H:i', $row['checktime']);
					} else {
						if ($row['status'] == -1) {
							$row['dealtime'] = date('Y-m-d H:i', $row['invalidtime']);
							$row['statusstr'] = '无效';
						}
					}
				}
			}
		}
		unset($row);
		show_json(1, array('total' => $total, 'list' => $list, 'pagesize' => $psize, 'commissioncount' => number_format($commissioncount, 2)));
	} else {
		if ($operation == 'detail') {
			$id = intval($_GPC['id']);
			$apply = pdo_fetch('select * from ' . tablename('ewei_shop_commission_apply') . ' where id=:id and `mid`=:mid and uniacid=:uniacid limit 1', array(':id' => $id, ':mid' => $mid, ':uniacid' => $uniacid));
			if (empty($apply)) {
				show_json(0, array('message' => '未找到提现申请!'));
			}
			$orderids = iunserializer($apply['orderids']);
			if (!is_array($orderids) || count($orderids) <= 0) {
				show_json(0, array('message' => '未找到订单信息!'));
			}
			show_json(1, array('apply' => $apply));
		} else {
			if ($operation == 'detail_order') {
				$id = intval($_GPC['id']);
				$pindex = max(1, intval($_GPC['page']));
				$psize = 5;
				$apply = pdo_fetch('select orderids from ' . tablename('ewei_shop_commission_apply') . ' where id=:id and `mid`=:mid and uniacid=:uniacid limit 1', array(':id' => $id, ':mid' => $mid, ':uniacid' => $uniacid));
				if (empty($apply)) {
					show_json(0, array('message' => '未找到提现申请!'));
				}
				$orderids = iunserializer($apply['orderids']);
				if (!is_array($orderids) || count($orderids) <= 0) {
					show_json(0, array('message' => '未找到订单信息!'));
				}
				$ids = array();
				foreach ($orderids as $o) {
					$ids[] = $o['orderid'];
				}
				$list = pdo_fetchall("select o.id,o.agentid, o.ordersn,o.price,o.goodsprice, o.dispatchprice,o.createtime, o.paytype from " . tablename('ewei_shop_order') . ' o ' . " left join " . tablename('ewei_shop_member') . " m on o.openid = m.openid " . " where  o.id in ( " . implode(",", $ids) . " ) LIMIT " . ($pindex - 1) * $psize . ',' . $psize);
				$totalcommission = 0;
				$totalpay = 0;
				foreach ($list as &$row) {
					$ordercommission = 0;
					$orderpay = 0;
					foreach ($orderids as $o) {
						if ($o['orderid'] == $row['id']) {
							$row['level'] = $o['level'];
							break;
						}
					}
					$condition = "";
					$status = trim($_GPC['status']);
					if ($status != '') {
						$condition .= ' and status=' . intval($status);
					}
					$goods = pdo_fetchall("SELECT og.id,og.goodsid,g.thumb,og.price,og.total,g.title,og.optionname," . "og.commission1,og.commission2,og.commission3," . "og.status1,og.status2,og.status3," . "og.content1,og.content2,og.content3 from " . tablename('ewei_shop_order_goods') . " og" . " left join " . tablename('ewei_shop_goods') . " g on g.id=og.goodsid  " . " where og.orderid=:orderid and og.uniacid = :uniacid order by og.createtime  desc ", array(':uniacid' => $_W['uniacid'], ':orderid' => $row['id']));
					$goods = set_medias($goods, 'thumb');
					foreach ($goods as &$g) {
						if ($row['level'] == 1) {
							$commission = iunserializer($g['commission1']);
							$g['commission'] = isset($commission['level' . $agentLevel['id']]) ? $commission['level' . $agentLevel['id']] : $commission['default'];
							$totalcommission += $g['commission'];
							$ordercommission += $g['commission'];
							if ($g['status1'] >= 2) {
								$totalpay += $g['commission'];
								$orderpay += $g['commission'];
							}
						}
						if ($row['level'] == 2) {
							$commission = iunserializer($g['commission2']);
							$g['commission_pay'] = 0;
							$g['commission'] = isset($commission['level' . $agentLevel['id']]) ? $commission['level' . $agentLevel['id']] : $commission['default'];
							$totalcommission += $g['commission'];
							$ordercommission += $g['commission'];
							if ($g['status2'] >= 2) {
								$g['commission_pay'] = $g['commission'];
								$totalpay += $g['commission'];
								$orderpay += $g['commission'];
							}
						}
						if ($row['level'] == 3) {
							$commission = iunserializer($g['commission3']);
							$g['commission'] = isset($commission['level' . $agentLevel['id']]) ? $commission['level' . $agentLevel['id']] : $commission['default'];
							$totalcommission += $g['commission'];
							$ordercommission += $g['commission'];
							if ($g['status3'] >= 2) {
								$totalpay += $g['commission'];
								$orderpay += $g['commission'];
							}
						}
						$status = $g['status' . $row['level']];
						if ($status == 1) {
							$g['statusstr'] = '待审核';
							$g['dealtime'] = date('Y-m-d H:i', $row['applytime' . $row['level']]);
						} else {
							if ($status == 2) {
								$g['statusstr'] = '待打款';
								$g['dealtime'] = date('Y-m-d H:i', $row['checktime' . $row['level']]);
							} else {
								if ($status == 3) {
									$g['statusstr'] = '已打款';
									$g['dealtime'] = date('Y-m-d H:i', $row['checktime' . $row['level']]);
								} else {
									if ($status == -1) {
										$g['dealtime'] = date('Y-m-d H:i', $row['invalidtime' . $row['level']]);
										$g['statusstr'] = '无效';
									}
								}
							}
						}
						$g['status'] = $status;
						$g['content'] = $g['content' . $row['level']];
						$g['level'] = $row['level'];
						if ($row['level'] == 1) {
							$g['level'] = '一';
						} else {
							if ($row['level'] == 2) {
								$g['level'] = '二';
							} else {
								if ($row['level'] == 3) {
									$g['level'] = '三';
								}
							}
						}
					}
					unset($g);
					$row['goods'] = $goods;
					$row['ordercommission'] = $ordercommission;
					$row['orderpay'] = $orderpay;
				}
				unset($row);
				show_json(1, array('list' => $list, 'pagesize' => $psize, 'totalcommission' => $totalcommission));
			}
		}
	}
}
if ($operation == 'display') {
	include $this->template('log');
}
if ($operation == 'detail') {
	include $this->template('log_detail');
}